package com.jf.demo.composeapp

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Person
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jf.demo.composeapp.ui.theme.ComposeAppTheme
import com.jf.demo.composeapp.ui.view.MyContent
import com.jf.demo.composeapp.ui.view.MyTopBarCenter

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MainContent(context = this)
        }
    }
}

@Composable
fun MainContent(context: Context? = null) {
    MyContent(
        topBar = { MyTopBarCenter("Home", Icons.Default.Menu) {} },
        bottomBar = { MyBottomBar() },
    ) {
        val greetingTxt = remember {
            mutableStateOf("Android")
        }
        Greeting(greetingTxt) {
            greetingTxt.value = "clicked!"
            context?.let {
                Toast.makeText(it, "点击了Greeting", Toast.LENGTH_SHORT).show()
                it.startActivity(Intent(it, ListActivity::class.java))
            }
        }
    }
}

@Composable
private fun MyBottomBar() {
    var selectIndex by remember { mutableStateOf(0) }
    BottomAppBar(
        backgroundColor = MaterialTheme.colors.primary
    ) {
        BottomNavigationItem(
            selected = selectIndex == 0,
            onClick = { selectIndex = 0 },
            icon = { Icon(imageVector = Icons.Default.Home, contentDescription = "Home") },
            label = { Text("Home") },
            selectedContentColor = Color.Green,
            unselectedContentColor = Color.White
        )
        BottomNavigationItem(
            selected = selectIndex == 1,
            onClick = { selectIndex = 1 },
            icon = { Icon(imageVector = Icons.Default.Person, contentDescription = "Me") },
            label = { Text("Me") },
            selectedContentColor = Color.Green,
            unselectedContentColor = Color.White
        )
    }
}


@Composable
fun Greeting(name: MutableState<String>, onClick: () -> Unit) {
    Surface(color = MaterialTheme.colors.primary) {
        Text(
            text = "Hello ${name.value}!",
            color = Color.Green,
            modifier = Modifier
                .padding(12.dp)
                .clickable(onClick = onClick)
        )
    }
}

@Preview
@Composable
fun Preview() {
    val context = LocalContext.current
    ComposeAppTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            MainContent(context)
        }
    }
}